from datetime import date,timedelta

from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from goods.models import GoodsVisitCount
from meiduo_admin.serializers.statistical import GoodsSerializer
from users.models import User


class GoodsDayView(APIView):

    def get(self,request):
        # 获取当天日期
        now_date=date.today()
        # 获取当天访问的商品分类数量信息
        data=GoodsVisitCount.objects.filter(date=now_date)
        # 序列化返回分类数量
        ser=GoodsSerializer(data,many=True)

        return Response(ser.data)


class UserMonthCountView(APIView):
    """
        月增用户统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当天日期
        now_date = date.today()
        # 2、获取一个月之前的日期
        begin_date = now_date - timedelta(days=59)
        data_list = []
        for i in range(60):
            # 起始日期
            index_date = begin_date + timedelta(days=i)
            # 第二天的日期(起始日期的下一天日期)
            next_date = begin_date + timedelta(days=i + 1)
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            data_list.append({
                'count': count,
                'date': index_date
            })
        return Response(data_list)


class UserOrderCountView(APIView):
    """获取下单用户数量"""
    permission_classes = [IsAdminUser]

    def get(self, request):

        # 获取当前日期 方便查看数据，用户只有一个
        # now_date = date.today()
        # 获取用户数量
        # 连表过滤查询 模型类名小写__属性名称__运算符=值
        count = len(set(User.objects.filter(orderinfo__create_time__gte='2020-02-19')))
        # 返回响应
        return Response(
            {
                "count": count,
                "date": '2020-02-19'
            })


class UserActiveCountView(APIView):
    """获取日增用户"""
    permission_classes = [IsAdminUser]

    def get(self, request):

        # 获取当前日期
        now_date = date.today()
        # 获取用户数量
        count = User.objects.filter(last_login__gte=now_date).count()
        # 返回响应
        return Response(
            {
                "count": count,
                "date": now_date
            })


class UserDayCountView(APIView):
    """获取日增用户"""
    permission_classes = [IsAdminUser]

    def get(self, request):

        # 获取当前日期
        now_date = date.today()
        # 获取用户数量
        count = User.objects.filter(date_joined__gte=now_date).count()
        # 返回响应
        return Response(
            {
                "count": count,
                "date": now_date
            }

        )


class UserCountView(APIView):
    """
        用户总量统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当天日期
        now_date = date.today()
        # 2、获取用户总量
        count = User.objects.all().count()
        # 3、返回结果
        return Response({
            'date': now_date,
            'count': count
        })